<template>
    <section>




        <!--新增界面-->

        <el-form :model="addForm" label-width="100px" :rules="addFormRules" ref="addForm">
            <el-form-item label="告警点名称">
                <el-input v-model="addForm.name"></el-input>
            </el-form-item>
            <el-form-item label="告警类型">
                <el-select v-model="addForm.alarmTypeId" placeholder="请选择告警类型">
                    <el-option label="类型1" value="1"></el-option>
                    <el-option label="类型2" value="2"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="数据模板ID">
                <el-select v-model="addForm.deviceDataRecipeId" placeholder="请选择数据模板ID">
                    <el-option label="区域一" value="1"></el-option>
                    <el-option label="区域二" value="2"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="数据模板数据点ID">
                <el-select v-model="addForm.deviceDataRecipeBodyId" placeholder="请选择数据模板数据点ID">
                    <el-option label="区域一" value="1"></el-option>
                    <el-option label="区域二" value="2"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="告警等级">
                <el-select v-model="addForm.level" placeholder="请选择告警等级">
                    <el-option label="区域一" value="1"></el-option>
                    <el-option label="区域二" value="2"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="数据点">
                <el-input v-model="addForm.register"></el-input>
            </el-form-item>
            <el-form-item label="触发条件">
                <el-input v-model="addForm.triggerCondition"></el-input>
            </el-form-item>
            <el-form-item label="触发最小值">
                <el-input v-model="addForm.inputMin"></el-input>
            </el-form-item>
            <el-form-item label="触发最大值">
                <el-input v-model="addForm.inputMax"></el-input>
            </el-form-item>
            <el-form-item label="告警方式">
                <el-radio-group v-model="addForm.alarmMode">
                    <el-radio label="1">微信</el-radio>
                    <el-radio label="2">短信</el-radio>
                    <el-radio label="3">邮件</el-radio>
                </el-radio-group>
            </el-form-item>
            <!-- <el-form-item label="是否存储数据">
                <el-radio-group v-model="addForm.isSave">
                    <el-radio label="0">否</el-radio>
                    <el-radio label="1">是</el-radio>
                </el-radio-group>
            </el-form-item> -->
            <el-form-item label="启用状态">
                <el-radio-group v-model="addForm.status">
                    <el-radio label="0" value="0" checked>不启用</el-radio>
                    <el-radio label="1" value="1">启用</el-radio>
                </el-radio-group>
            </el-form-item>
            <el-form-item label="告警推送内容">
                <el-input type="textarea" v-model="addForm.customContent"></el-input>
            </el-form-item>
            <el-form-item label="恢复正常推送内容">
                <el-input type="textarea" v-model="addForm.customContentNormal"></el-input>
            </el-form-item>
            <el-form-item label="备注" prop="remark">
                <el-input type="textarea" v-model="addForm.remark"></el-input>
            </el-form-item>

            <el-form-item label="告警组">

                <el-transfer v-model="addForm.alarmUserLists" :titles="['未选', '已选']" :button-texts="['到左边', '到右边']" :data="userlists" style="margin-left:30px;padding-bottom:20px;"></el-transfer>

            </el-form-item>
            </el-form-item>
            <el-form-item label="告警人">
                <el-transfer v-model="addForm.alarmUserList" :data="userlist" style="margin-left:30px;padding-bottom:20px;"></el-transfer>
            </el-form-item>

        </el-form>
        <div slot="footer" class="dialog-footer" style="text-align: right;">
            <el-button @click.native="addFormVisible = false">取消</el-button>
            <el-button type="primary" @click.native="addSubmit" :loading="addLoading">提交</el-button>
        </div>

    </section>
</template>

<script>
    import {
        alarm_trigger_template_post,
        alarms_query_user,
        alarm_trigger_post,
        alarm_group_query,
        alarm_type_query
    } from '../../../api/api';

    export default {
        data() {
            const generateDatas = _ => {
                const data = [];
                let para = {};
                //NProgress.start();
                alarm_group_query(para).then((res) => { this.begCode (res.code);
                    // this.total = res.data.totalCount;
                    if (res.code == 0) {


                        this.query = res.data.list;
                        for (let i in res.data.list) {
                            data.push({
                                key: res.data.list[i].id,
                                label: res.data.list[i].name
                            });
                        }
                    } else {
                        this.$router.push({
                            path: '/login'
                        });
                    }
                    //NProgress.done();
                });
                return data;
            };
            const generateData = _ => {
                const data = [];
                let para = {};
                //NProgress.start();
                alarms_query_user(para).then((res) => { this.begCode (res.code);
                    // this.total = res.data.totalCount;
                    if (res.code == 0) {


                        this.query = res.data.list;
                        for (let i in res.data.list) {
                            data.push({
                                key: res.data.list[i].id,
                                label: res.data.list[i].name
                            });
                        }
                    } else {
                        this.$router.push({
                            path: '/login'
                        });
                    }
                    //NProgress.done();
                });
                return data;
            };
            return {
                alarmUserList:{},//告警组ID
                alarmUserLists:{},//告警人ID
                alarmTypeValue: '',
                userlist: generateData(),
                userlists: generateDatas(),

                addLoading: false,
                addFormRules: {
                    name: [{
                        required: true,
                        message: '请输入姓名',
                        trigger: 'blur'
                    },{ min: 1, max: 20, message: '长度在 1 到 20 个字符', trigger: 'blur' }],

                },
                //新增界面数据
                addForm: {
                    alarmTypeId: '',
                    name: '',
                    deviceDataRecipeId: '',
                    deviceDataRecipeBodyId: '',
                    level: '',
                    register: '',
                    triggerCondition: '',
                    inputMin: '',
                    inputMax: '',
                    alarmMode: '0',
                    isSave: '0',
                    status: '0',
                    customContent: '',
                    customContentNormal: '',
                    remark: '',
                    alarmTriggerGroupList:[],
                    alarmUserList:[],
                    alarmUserLists:[]
                }

            }
        },
        methods: {

            //新增
            addSubmit: function () {
                this.$refs.addForm.validate((valid) => {
                    if (valid) {
                        this.$confirm('确认提交吗？', '提示', {}).then(() => {
                            for(let i in this.addForm.alarmUserLists){
                                this.addForm.alarmTriggerGroupList.push({"type":2,"alarmTypeValue":this.addForm.alarmUserLists[i]});
                            }
                            for(let i in this.addForm.alarmUserList){
                                this.addForm.alarmTriggerGroupList.push({"type":1,"alarmTypeValue":this.addForm.alarmUserList[i]});
                            }
                            this.addLoading = true;
                            //NProgress.start();
                            let para = Object.assign({}, this.addForm);
                           
                            alarm_trigger_template_post(para).then((res) => { this.begCode (res.code);
                                if (res.code == 0) {

                                    this.addLoading = false;
                                    //NProgress.done();
                                    this.$message({
                                        message: '提交成功',
                                        type: 'success'
                                    });
                                    this.$refs['addForm'].resetFields();
                                    this.addFormVisible = false;

                                }

                            });
                        });
                    }
                });
            }


        },
        mounted() {}
    }
</script>

<style scoped>

</style>